package cn.tedu.antconlonysever.contract.controller;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.JsonResult;

import cn.tedu.antconlonysever.contract.pojo.param.OrderAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.OrderUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.*;
import cn.tedu.antconlonysever.contract.service.IOrderService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/order")
@Api(tags = "02.订单管理")
@Slf4j
@Validated
public class OrderController {
    @Autowired
    private IOrderService service;
    @PostMapping("/add-new")
    @ApiOperation("添加订单")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid OrderAddNewParam orderAddNewParam){
        log.debug("开始处理【添加订单】的请求，参数：{}", orderAddNewParam);
        service.addNew(orderAddNewParam);
        return JsonResult.ok();
    }
    @PostMapping("/delete")
    @ApiOperation("删除订单")
    @PreAuthorize("hasAuthority('/customer/order/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除订单失败，请提交合法的ID值！")
                                         @RequestParam Long id){
        log.debug("开始处理【根据ID删除订单】的请求，参数：{}", id);
        service.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改订单")
    @PreAuthorize("hasAuthority('/customer/order/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "订单id",required = true,dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1,message = "请提交有效的订单ID值!") Long id,
                                     @Valid OrderUpdateParam orderUpdateParam){
        log.debug("开始处理【修改订单】的请求，新数据：{}",  orderUpdateParam);
        service.updateInfoById(id,orderUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据id查询订单详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "订单id",required = true,dataType = "long")
    })
    public JsonResult getStandardById( @Range(min = 1,message = "根据id查询订单详情失败！请输入合法的数据参数，")
                                       @RequestParam Long id){
        log.debug("开始处理【根据id查询订单详情】的请求，iD：{}，", id);
        OrderStandardVO result = service.getStandardById(id);
        return JsonResult.ok(result);

    }
    @GetMapping("/list")
    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询订单列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理【查询订单列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<OrderListVO> pageData = service.list(page);
        return JsonResult.ok(pageData);
    }
}
